package net.hn.hnms.biz.safety.water.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.hn.hnms.biz.safety.water.company.domain.CGKAlarmVO;
import net.hn.hnms.biz.safety.water.company.service.CompanyService;
import net.hn.hnms.biz.safety.water.domain.HolealAbnormalPo;
import net.hn.hnms.biz.safety.water.mapper.HolealAbnormalMapper;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.redis.domain.SysOrganization;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class CompanyServiceImpl implements CompanyService {


    /**
     *  长观孔报警Mapper
     */
    @Resource
    private HolealAbnormalMapper holealAbnormalMapper;

    /**
     * 获取长观孔报警列表
     * @param companyCode
     * @return
     */
    @Override
    public List<CGKAlarmVO> getTodayCGKAlarmList(String companyCode) {
        if (StringUtils.isEmpty(companyCode)) {
            throw new HuaNengException(Constants.FAIL, "公司编码不可以为空");
        }
        //获取当前公司的所有煤矿
        SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);
        if (sysOrganizationVo == null) {
            throw new HuaNengException(Constants.FAIL, "无法获当前公司的子煤矿信息，请检查词典"+companyCode);
        }
        List<CGKAlarmVO> voList = new ArrayList<>();
        for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
            //遍历当前公司的所有煤矿
            List<CGKAlarmVO> alarmInfoByMineCode = getAlarmInfoByMineCode(organizationVo.getOrgCode());
            for (CGKAlarmVO cgkAlarmVO : alarmInfoByMineCode) {
                voList.add(cgkAlarmVO);
            }
        }
        return voList;
    }

    /**
     * 根据煤矿编码获取今天的长观孔报警信息
     * @param mineCode
     * @return
     */
    public List<CGKAlarmVO> getAlarmInfoByMineCode(String mineCode) {
        //获取当前煤矿信息
        SysOrganization sysOrganization = RedisUtils.getCacheObject(Constants.SYS_CODE_KEY + mineCode);
        if (sysOrganization == null) {
            throw new HuaNengException(Constants.FAIL, "煤矿编码出错，请检查词典" + mineCode);

        }
        LambdaQueryWrapper<HolealAbnormalPo> wrapper = new LambdaQueryWrapper<>();
        // 获取当前日期
        LocalDate today = LocalDate.now();
        //今天的日期
        wrapper.apply("DATE(data_time) = '" + today + "'");
        //当前煤矿编码
        wrapper.eq(HolealAbnormalPo::getMineCode, mineCode);
        List<HolealAbnormalPo> holealAbnormalPos = holealAbnormalMapper.selectList(wrapper);
        if (holealAbnormalPos.isEmpty()) {
            return new ArrayList<>();
        }
        return holealAbnormalPos.stream().map(t->{
            CGKAlarmVO cgkAlarmVO = new CGKAlarmVO();
            //测点编码
            cgkAlarmVO.setPointCode(t.getPorintCode());
            //煤矿信息
            cgkAlarmVO.setMineCode(t.getMineCode());
            cgkAlarmVO.setMineName(sysOrganization.getOrgName());
            //长观孔位置
            cgkAlarmVO.setPointLocation(t.getSensorLocaltion());
            //报警类型
            cgkAlarmVO.setAlarmType(t.getExceptionType());
            //报警等级(蓝黄橙红)
            cgkAlarmVO.setAlarmLevel(t.getWarningLevel().toString());
            //报警开始时间
            cgkAlarmVO.setBeginTime(t.getStartTime());
            //报警结束时间
            cgkAlarmVO.setEndTime(t.getEndTime());
            //异常期间最大值
            cgkAlarmVO.setValue(t.getSensorValue());
            //异常内容
            cgkAlarmVO.setAbnormalCause(t.getExceptionCause());
            //报警时长
            Duration between = Duration.between(t.getStartTime().toInstant(), t.getEndTime().toInstant());
            cgkAlarmVO.setDuration(minutConversion(new BigDecimal(between.toMinutes())));
            return cgkAlarmVO;
        }).collect(Collectors.toList());
    }
    /**
     *  传递一个数值类型 分钟数
     * @param minutes
     * @return 返回 **天**时**秒 格式的数据
     */
    public String minutConversion(BigDecimal minutes) {
//            int minutes = 1500;
//            int days = minutes / (24 * 60); // 计算天数
//            int hours = (minutes % (24 * 60)) / 60; // 计算小时数
//            int remainingMinutes = minutes % 60; // 计算剩余的分钟数
//            System.out.println(days + "天 " + hours + "小时 " + remainingMinutes + "分钟");

        //minutes 带小数的分钟数
        BigDecimal daysDecimal = minutes.divide(new BigDecimal("1440"), 0, BigDecimal.ROUND_DOWN); // 计算天数
        int days = daysDecimal.intValue();
        BigDecimal remainingMinutesDecimal = minutes.remainder(new BigDecimal("1440")); // 计算剩余分钟数

        BigDecimal hoursDecimal = remainingMinutesDecimal.divide(new BigDecimal("60"), 0, BigDecimal.ROUND_DOWN); // 计算小时数
        int hours = hoursDecimal.intValue();

        BigDecimal remainingMinutes = remainingMinutesDecimal.remainder(new BigDecimal("60")); // 计算剩余分钟数

        return days + "天" + hours + "小时" + remainingMinutes + "分钟";
    }
}
